﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace VideoLib.Utils
{
    public class JsonHelper
    {
        public static string Serialize<T>(T obj)
        {
            return JsonSerializer.Serialize(obj);
        }

        public static T? Deserialize<T>(string json)
        {
            return JsonSerializer.Deserialize<T>(json);
        }

        /// <summary>
        /// 将Json字符串保存到文件
        /// </summary>
        public static void SaveJsonToFilee<T>(T obj, string filePath)
        {
            var str = Serialize<T>(obj);
            System.IO.File.WriteAllText(filePath, str);
        }

        /// <summary>
        /// 从文件中读取Json字符串
        /// </summary>
        public static T? ReadJsonFromFile<T>(string filePath)
        {
            var str = System.IO.File.ReadAllText(filePath);
            return Deserialize<T>(str);
        }
    }
}
